<template>
	<view class="search">
		<musicHeader title="搜索" :icon="true" :iconblack="true"></musicHeader>
		<view class="container">
			<scroll-view scroll-y="true">
				<view class="search-component">
					<text class="iconfont icon-chazhao"></text>
					<input type="text" placeholder="搜索歌曲" v-model="searchWord" @confirm="handleToSearch"
						@input="handleToSuggest" />
					<text v-show="searchType != 1" class="iconfont icon-guanbi" @tap="handleToClose"></text>
				</view>
				<block v-if="searchType == 1">
					<view class="search-history">
						<view class="search-history-head">
							<text>历史记录</text>
							<text class="iconfont icon-icon1" @tap="handleToClear"></text>
						</view>
						<view class="search-history-list">
							<view v-for="(item,index) in historyList" :key="index" @tap="handleToWord(item)">{{ item }}
							</view>
						</view>
					</view>
					<view class="search-hot">
						<view class="search-hot-head">热搜榜</view>
						<view class="search-hot-item" v-for="(item, index) in searchHot" :key="index"
							@tap="handleToWord(item.searchWord)">
							<view class="search-hot-top">{{ index + 1 }}</view>
							<view class="search-hot-word">
								<view>
									{{ item.searchWord }}
									<image :src="item.iconUrl" mode="aspectFit"></image>
								</view>
								<view>{{ item.content }}</view>
							</view>
							<text class="search-hot-count">{{ item.score }}</text>
						</view>
					</view>
				</block>

				<block v-else-if="searchType == 2">
					<view class="search-result">
						<view class="search-result-item" v-for="(item, index) in searchList" :key="index"
							@tap="handleToDetail(item.id)">
							<view class="search-result-word">
								<view>{{ item.name }}</view>
								<view>{{ item.artists[0].name }} - {{ item.album.name }}</view>
							</view>
							<text class="iconfont icon-bofang"></text>
						</view>
					</view>
				</block>
				<block v-else-if="searchType == 3">
					<view class="search-suggest">
						<view class="search-suggest-title">搜索“{{ searchWord }}”</view>
						<view class="search-suggest-item" v-for="(item,index) in suggestList" :key="index"
							@tap="handleToWord(item.keyword)">
							<text class="iconfont icon-chazhao"></text>
							{{ item.keyword }}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import musicHeader from '../../components/musicHeader.vue';
	import {
		searchHot,
		searchWord,
		searchSuggest
	} from '../../common/api.js'

	export default {
		data() {
			return {
				searchHot: [],
				searchWord: '',
				historyList: [],
				searchType: 1,
				searchList: [],
				suggestList: []
			};
		},
		components: {
			musicHeader
		},
		onLoad() {
			searchHot().then((res) => {
				if (res.data.code == 200) {
					this.searchHot = res.data.data;
				}
			});
			uni.getStorage({
				key: 'searchHistory',
				success: (res) => {
					this.historyList = res.data;
				}
			});
		},
		methods: {
			handleToWord(word) {
				this.searchWord = word;
				this.handleToSearch();
			},
			handleToSearch(word) {
				this.historyList.unshift(this.searchWord);
				this.historyList = [...new Set(this.historyList)];
				if (this.historyList.length > 10) {
					this.historyList.length = 10;
				}
				uni.setStorage({
					key: 'searchHistory',
					data: this.historyList
				});
				this.getSearchList(this.searchWord);
			},
			handleToClear() {
				uni.removeStorage({
					key: 'searchHistory',
					success: () => {
						this.historyList = [];
					}
				});
			},
			getSearchList(word) {
				searchWord(word).then((res) => {
					console.log(res);
					if (res.data.code == 200) {
						this.searchList = res.data.result.songs;
						this.searchType = 2;
					}
				});
			},
			handleToClose() {
				this.searchWord = '';
				this.searchType = 1;
			},
			handleToDetail(songId) {
				uni.navigateTo({
					url: '/pages/detail/detail?songId=' + songId
				})
			},
			handleToSuggest(ev) {
				let value = ev.detail.value;
				if (!value) {
					this.searchType = 1;
					return;
				}
				searchSuggest(value).then((res) => {
					if (res.data.code == '200') {
						this.suggestList = res.data.result.allMatch;
						this.searchType = 3;
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	@import '../../static/iconfont/iconfont.css';

	.search-component {
		display: flex;
		align-items: center;
		height: 70rpx;
		margin: 70rpx 30rpx 50rpx 30rpx;
		background: #f7f7f7;
		border-radius: 50rpx;

		text {
			font-size: 34rpx;
			margin: 0 26rpx;
		}

		input {
			font-size: 26rpx;
			flex: 1;
		}
	}

	.search-history {
		margin: 0 30rpx 50rpx 30rpx;
		font-size: 26rpx;

		.search-history-head {
			display: flex;
			justify-content: space-between;
			margin-bottom: 36rpx;
		}

		.search-history-list {
			display: flex;
			flex-wrap: wrap;

			view {
				padding: 16rpx 28rpx;
				border-radius: 40rpx;
				margin: 0 30rpx 30rpx 0;
				background: #f7f7f7;
			}
		}
	}

	.search-hot {
		margin: 0 36rpx;
		font-size: 26rpx;

		.search-hot-head {
			margin-bottom: 36rpx;
		}

		.search-hot-item {
			display: flex;
			align-items: center;
			margin-bottom: 58rpx;

			.search-hot-top {
				color: #fb2222;
				width: 60rpx;
				margin-left: 8rpx;
			}

			.search-hot-word {
				flex: 1;

				view:nth-child(1) {
					font-size: 30rpx;
					color: black;
					font-weight: bold;
					margin-bottom: 4rpx;
				}

				view:nth-child(2) {
					font-size: 24rpx;
					color: #878787;
				}


				image {
					width: 48rpx;
					height: 22rpx;
				}
			}

			.search-hot-count {
				font-size: 24rpx;
				color: #b6b6b6;
			}
		}
	}

	.search-result {
		border-top: 2rpx #e4e4e4 solid;
		padding: 30rpx;

		.search-result-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-bottom: 30rpx;
			margin-bottom: 30rpx;

			.search-result-word {

				view:nth-child(1) {
					font-size: 28rpx;
					color: #235790;
					margin-bottom: 12rpx;
				}

				view:nth-child(2) {
					font-size: 22rpx;
					color: #898989;
				}
			}

			text {
				font-size: 50rpx;
			}
		}
	}

	.search-suggest {
		border-top: 2rpx #e4e4e4 solid;
		padding: 30rpx;
		font-size: 26rpx;

		.search-suggest-title {
			color: #4574a5;
			margin-bottom: 74rpx;
		}

		.search-suggest-item {
			color: #5d5d5d;
			margin-bottom: 74rpx;

			text {
				color: #bdbdbd;
				font-size: 26rpx;
				margin-right: 28rpx;
			}
		}
	}
</style>